package com.taptech.doufu.ui.view.choice;
import android.content.Context;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.choice.NewUserGiftModel;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.weex.http.TFHttpError;
import com.taptech.doufu.weex.http.TFHttpResponse;
import com.taptech.doufu.weex.http.TFHttpUtil;

import java.util.HashMap;

public class ChoiceTopicNewUserView extends LinearLayout {

    private Context mContext;
    private boolean isLoading = false;
    private TextView tvBtn;
    private int status;

    public ChoiceTopicNewUserView(Context context) {
        super(context);
    }

    public ChoiceTopicNewUserView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ChoiceTopicNewUserView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ChoiceTopicNewUserView(Context context, final NewUserGiftModel model,int layoutId) {
        this(context);
        mContext = context;
        if (model == null) {
            return;
        }
        status = model.getStatus();
        View view = LayoutInflater.from(context).inflate(layoutId, null);
        TextView tvTitle = view.findViewById(R.id.tvTitle);
        TextView tvSubTitle = view.findViewById(R.id.tvSubTitle);
        tvBtn = view.findViewById(R.id.tvBtn);
        ImageView ivIcon = view.findViewById(R.id.ivIcon);
        GlideUtil.displayImage(ivIcon,model.getIcon());
        tvTitle.setText(model.getTitle());
        tvSubTitle.setText(model.getSub_title());
        tvBtn.setText(model.getBtn());
        tvBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (status == 0) {
                    getGift(model.getUrl());
                }else{
                    //跳转
                    SimpleWeexActivity.startActivity(mContext,"vue/newVip/vipCenter.js?needLogin=1");
                }
            }
        });
        addView(view);
    }

    private void getGift(String url) {
        if (isLoading) {
            return;
        }
        isLoading = true;
        TFHttpUtil.requestGet(url, new HashMap(), new TFHttpResponse() {
            @Override
            public void onResponse(JSONObject json, TFHttpError error) {
                isLoading = false;
                try {
                    JSONObject data = json.getJSONObject("data");
                    if (json.getInteger("status") == 0) {
                        String note = data.getString("note");
                        int affected = data.getInteger("affected");
                        if (affected > 0) {
                            if (!TextUtils.isEmpty(note)) {
                                Toast.makeText(mContext,note,Toast.LENGTH_LONG).show();
                            }else{
                                Toast.makeText(mContext,"领取成功",Toast.LENGTH_LONG).show();
                            }
                            tvBtn.setText("查看礼包");
                            status = 1;
                        }else{
                            if (!TextUtils.isEmpty(note)) {
                                Toast.makeText(mContext,note,Toast.LENGTH_LONG).show();
                            }else{
                                Toast.makeText(mContext,"领取失败",Toast.LENGTH_LONG).show();
                            }
                        }

                    }else{
                        String user_msg = json.getString("user_msg");
                        if (!TextUtils.isEmpty(user_msg)) {
                            Toast.makeText(mContext,user_msg,Toast.LENGTH_LONG).show();
                        }else{
                            Toast.makeText(mContext,"领取失败",Toast.LENGTH_LONG).show();
                        }
                    }

                } catch (Exception e) {
                    Toast.makeText(mContext,"领取失败",Toast.LENGTH_LONG).show();

                }

            }
        });
    }

}
